from django.contrib.auth.hashers import make_password
from rest_framework.serializers import ModelSerializer, ValidationError
from .models import *
import re


class UserSer(ModelSerializer):

    def validate_phone(self, phone):
        if not re.match(r'^1[3-9]\d{9}$', phone):
            raise ValidationError(detail='无效手机号', code=400)
        return phone

    def validate(self, attrs):
        attrs['password'] = make_password(attrs['password'])
        return attrs

    class Meta:
        model = UserProfile
        fields = '__all__'
